# file:  appcfix.r   ssw 12/1/86
#	convert wide APPC to APPA format, but without P or PRP column
# usage:  "appcfix < appc100 > appc50"
# modified 10/29/90 for 60-digit output format:  "appcfix < appc100 > appc60"

define (NEWLINE,char(10))
define (BLANK, " ")
define (EOF, -1)
define (STDERR, 0)
define (APPC, 5)		# input:   old appendix c
define (APPCNEW, 6)		# output:  new version of appc
define (SZ,60)			# digits in output format
define (TZ,59)			# digits - 1 in output format

implicit undefined (a-z)	# turn off implicit typing
integer fgetc, ctoi	# functions
integer length, upcnt, io, io2, eofflg, blank
character cnum(400), inp(100), clbl8(8), ch1r

# copy header line from appc to appcnew

repeat {
    eofflg = fgetc(APPC, ch1r)
    if (eofflg == EOF) {
	write (STDERR, '("EOF in first line of APPC")')
	stop
	}
    call fputc(APPCNEW, ch1r) 
    }
until (ch1r == NEWLINE)

repeat {
	
	# read line from APPC
	
	do io2 = 1, 6 {
	    eofflg = fgetc(APPC, inp(io2))
	    if (eofflg == EOF) {
		write (STDERR, 970) upcnt
		970 format ('appc EOF after', i10, 'update lines; end')
		stop
		}
	    }
	upcnt = upcnt + 1
	io2 = 1 ; length = ctoi(inp, io2)
	do io2 = 1, 8 
	    eofflg = fgetc(APPC, clbl8(io2))
	do io2 = 1, 2
	    eofflg = fgetc(APPC, ch1r)
	for (io2=1; io2<=length; io2=io2+1) {
	    eofflg = fgetc(APPC, cnum(io2))
	    if (cnum(io2) == NEWLINE) {
		io2 = io2 - 1
		do io = 1, 16
		    eofflg = fgetc(APPC, ch1r)
		}
	    }
	eofflg = fgetc(APPC, ch1r) # read NEWLINE

	do io = 1, length {
	  if((io<length)&((mod(length-io,SZ)==TZ)|(io==1))){
		if (io == 1) {
		    call putdec(APPCNEW, length, 4)
		    call fputc(APPCNEW, BLANK)
		  # put out label, removing blanks after third character (,)
		    do io2 = 1, 3
		        call fputc(APPCNEW, clbl8(io2))
		    blank = 0
		    while (clbl8(4 + blank) == BLANK) 
			blank = blank + 1
		    do io2 = 4 + blank, 8
			call fputc(APPCNEW, clbl8(io2))
		    do io2 = 1, 4 + blank
		        call fputc(APPCNEW, BLANK)
		    if ((length > SZ) & (mod(length, SZ) > 0)) {
	    		do io2 = 1, SZ - mod(length, SZ) {
				call fputc(APPCNEW, BLANK)
				if (mod(io2, 10) == 0)
					call fputc(APPCNEW, BLANK)
		    		}
			}
		    }
		else {
		    do io2 = 1, 17
		         call fputc(APPCNEW, BLANK)
		    }
		}
	  call fputc(APPCNEW, cnum(io))
	  if (mod(length - io, 10) == 0) {
		if (mod(length - io, SZ) == 0) {
		    if (io < length)
	    		call fputc(APPCNEW, NEWLINE)
		    }
		else 
			call fputc(APPCNEW,BLANK)
		}
	  }
	call fputc(APPCNEW, NEWLINE)
	}

end

subroutine putdec(fd,n,w)
character ch(79)
integer itoc,i,n,nd,w,fd
nd=itoc(n,ch,79)
for (i=nd+1; i<=w; i=i+1)
     call fputc(fd,BLANK)
for (i=1; i<=nd; i=i+1)
     call fputc(fd,ch(i))
return
end

integer function ctoi(in,i) # convert string at in(i) to integer, increment i
character in(1)
character*10 digits
integer index,d,i
data digits/"0123456789"/
while (in(i)==BLANK)
     i=i+1  # find the first non-blank
for (ctoi=0; in(i)!=char(0); i=i+1) {
     d=index(digits,in(i))
     if (d==0)  # if non-digit
          break
     ctoi=10*ctoi + d - 1
     }
return
end

integer function itoc(int,str,size) # convert integer int to char string in str
integer iabs, mod, d,i,int,intval,j,size  # copied from software tools
character k, str(size), digits(10)              # by Kernighan and Plauger
data digits /"0","1","2","3","4","5","6","7","8","9"/

intval=iabs(int) # get absolute value
str(1)=char(0)
i=1
repeat { # generate digits
     i=i+1
     d=mod(intval,10)
     str(i)=digits(d+1)
     intval=intval/10
     } until (intval==0|i>=size)
if (int<0 & i<size) {  # put in the sign
     i=i+1
     str(i)="-"
     }
itoc=i-1
for (j=1; j<i; j=j+1) {  # then reverse
     k=str(i)
     str(i)=str(j)
     str(j)=k
     i=i-1
     }
return
end

